package dataextract;
/*
 * questo codice confronta i dati PLS e le loro reference geografiche 
 * con una serie di punti di interesse a TO (raggio di 1500 metri) e stampa tali corrispondenze.
 * Inoltre perogni PLS vengono stampati un 0 oppure un 1 sotto la corrispondenza del luogo d'interesse specifico 
 * in una forma a matrice 
 * 
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.gps.utils.LatLonPoint;
import org.gps.utils.LatLonUtils;

public class ConfrontaPLS2POI {

	public static void main(String[] args) throws Exception{
		String path = "C:\\Users\\Alket\\codeindigo\\FlickrAPI\\TotalIMSI-1Settimana\\IMSIStranieri_di_una_sett2.csv";
		List<Event> eventlist = new ArrayList<Event>();
		loadData(path, eventlist);
		
		String pathPOI = "C:\\Users\\Alket\\codeindigo\\FlickrAPI\\PuntiInteresseTorino.txt";
		List<POI> poilist = new ArrayList<POI>();
		loadPOI(pathPOI, poilist);
		
		Map<String, List<String>>map = new HashMap<String, List<String>>();
		List<String>poilist_i = new ArrayList<String>();
		
		for (int i = 1; i < eventlist.size(); i++) {
			
			String pls = eventlist.get(i).usr;
			int imsi = eventlist.get(i).imsi;
			String plsimsi = pls+" "+imsi;
			double lat_pls = eventlist.get(i).lat;
			double lon_pls = eventlist.get(i).lon;
			
			LatLonPoint gp1 = new LatLonPoint(lat_pls, lon_pls);
			String pls_i = eventlist.get(i-1).usr;
		
			for (int j = 0; j < poilist.size(); j++) {
				String poiname = poilist.get(j).poiname;
				double lat_poi = poilist.get(j).lat;
				double lon_poi = poilist.get(j).lon;
				LatLonPoint gp2 = new LatLonPoint(lat_poi, lon_poi);
				double d = LatLonUtils.getHaversineDistance(gp1, gp2);	
				if(d<1500){
					poilist_i.add(poiname);
					//System.out.println(poiname);
				}
			}
			if(!(pls.equals(pls_i))){
				//continue;
				if(!(poilist_i.isEmpty())){
					Set<String> set = new HashSet<String>(poilist_i);
					List<String> setlist = new ArrayList<String>(set);
					map.put(plsimsi, setlist);
				}
			poilist_i =  new ArrayList<String>();
			}
			//else{
				//if(!(poilist_i.isEmpty())){
					//map.put(pls, poilist_i);
					//}
					//poilist_i =  new ArrayList<String>();
			//}
		}
		print(map, "POIPerPLSUserImsiFile_1sett.csv");
	}
	
	public static void loadData(String file, List<Event> list) throws Exception{
		
		 BufferedReader br = new BufferedReader(new FileReader(new File(file)));
			
			String line ;
			while((line = br.readLine())!= null){
				String [] riga = line.split(",");
				String usr = riga[0];
				int imsi = Integer.parseInt(riga[1]);
				long celllac = Long.parseLong(riga[2]);
				long ts = Long.parseLong(riga[3]);
				double lat = Double.parseDouble(riga[4]);
				double lon = Double.parseDouble(riga[5]);
				int raggio = Integer.parseInt(riga[6]);
				
				list.add(new Event(usr, imsi, celllac, ts, lat, lon, raggio));
				
	    	}br.close();
		
		
	}
	public static void loadPOI(String file, List<POI> list) throws Exception{
		
		 BufferedReader br = new BufferedReader(new FileReader(new File(file)));
			
			String line ;
			while((line = br.readLine())!= null){
				String [] riga = line.split(",");
				String usr = riga[0];
				double lat = Double.parseDouble(riga[1]);
				//System.out.println(lat);
				double lon = Double.parseDouble(riga[2]);
				
				list.add(new POI(usr, lat, lon));
				
	    	}br.close();	
	}
	 public static void print(Map<String, List<String>>map , String file)throws Exception{
	    	
	    	PrintWriter out = new PrintWriter(new FileWriter(file));
	    	for(String l: map.keySet()){
	    		out.print(l+": ");
	    		
					out.println(map.get(l));
					
				
	    	}
	    	out.close();
	    }
	 
	 public static void loadFilesEvents(String path, List<Event> list)throws Exception{
			
		
		 File Folder = new File(path);
		 File files[];
		 files = Folder.listFiles();
		
		try{

            if(files.length>1)
            {
             for(int i = 0;i<files.length; i++){
               
               BufferedReader br = new BufferedReader(new FileReader(files[i]));
            
               String line ;
   			while((line = br.readLine())!= null){
   				String [] riga = line.split(",");
   				String usr = riga[0];
   				int imsi = Integer.parseInt(riga[1]);
   				long celllac = Long.parseLong(riga[2]);
   				long ts = Long.parseLong(riga[3]);
   				double lat = Double.parseDouble(riga[4]);
   				double lon = Double.parseDouble(riga[5]);
   				int raggio = Integer.parseInt(riga[6]);
   				
   				list.add(new Event(usr, imsi, celllac, ts, lat, lon, raggio));
            }br.close();
           }
            }
		 }catch( Exception ex){
		    	ex.printStackTrace(); 
	  }
	}
}
